//
//  AutoLayout.swift
//  autolayout
//
//  Created by 岳挺 on 15/6/27.
//  Copyright © 2015年 yueting. All rights reserved.
//

import UIKit

extension UIView {
    
    ///紧贴父控件左上
    func clineToSuperViewLeftAndTopWithSize (viewSize : CGSize){
        self.translatesAutoresizingMaskIntoConstraints = false
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subview(\(viewSize.width))]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subview(\(viewSize.height))]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
    }
    
    ///紧贴父控件右上
    func clineToSuperViewRightAndTopWithSize (viewSize : CGSize){
        self.translatesAutoresizingMaskIntoConstraints = false
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(\(viewSize.width))]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subview(\(viewSize.height))]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
    }
    
    ///紧贴父控件左下
    func clineToSuperViewLeftAndBottomWithSize (viewSize : CGSize){
        self.translatesAutoresizingMaskIntoConstraints = false
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subview(\(viewSize.width))]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(\(viewSize.height))]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
    }


    ///紧贴父控件右下
    func clineToSuperViewRightAndBottomWithSize (viewSize : CGSize){
        self.translatesAutoresizingMaskIntoConstraints = false
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(\(viewSize.width))]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(\(viewSize.height))]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
    }
    
    ///跟父控件的位置关系的枚举
    enum Direction {
        case TopAndLeft
        case TopAndRight
        case BottomAndLeft
        case BottomAndRight
        
        func valueOfDirection(superView : UIView , viewSize : CGSize) -> UIEdgeInsets{
            let w = superView.bounds.size.width
            let h = superView.bounds.size.height
            
            switch self {
            case.TopAndLeft:
                return UIEdgeInsetsMake(0, 0, w - viewSize.width, h - viewSize.height)
            case.TopAndRight:
                return UIEdgeInsetsMake(0, w - viewSize.width, 0, h - viewSize.height)
            case.BottomAndLeft:
                return UIEdgeInsetsMake(h - viewSize.height, 0, w - viewSize.width, 0)
            case.BottomAndRight:
                return UIEdgeInsetsMake(h - viewSize.height, w - viewSize.width, 0, 0)
            }
        }
    }
    
    ///紧靠父控件
    func clineToSuperViewWithSize (direction :Direction , viewSize : CGSize){
        let edge = direction.valueOfDirection(self.superview!, viewSize: viewSize)
        
        self.translatesAutoresizingMaskIntoConstraints = false
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-\(edge.left)-[subview(\(viewSize.width))]-\(edge.right)-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-\(edge.top)-[subview(\(viewSize.height))]-\(edge.bottom)-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
    }
    
    ///位于父控件中心,x，y偏移父控件中心距离
    func locateInSuperViewCenter (viewSize : CGSize , centerOffSet : CGPoint){
        self.translatesAutoresizingMaskIntoConstraints = false
        self.superview!.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.superview!, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: centerOffSet.x))
        self.superview!.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.superview!, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: centerOffSet.y))
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(\(viewSize.width))]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(\(viewSize.height))]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
    }
    
    ///与父控件等大
    func equalToSuperViewSize (){
        self.translatesAutoresizingMaskIntoConstraints = false
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subview]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subview]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : self]))
    }
    
    ///上对齐左追随
    func alignTopAndLeftSideFollowOtherView (otherView : UIView , distance : CGFloat ,viewSize : CGSize){
        leftSideFollowOtherView(otherView, distance: distance, viewSize: viewSize, option: NSLayoutFormatOptions.AlignAllTop)
    }
    
    ///下对齐左追随
    func alignBottomAndLeftSideFollowOtherView (otherView : UIView , distance : CGFloat ,viewSize : CGSize){
        leftSideFollowOtherView(otherView, distance: distance, viewSize: viewSize, option: NSLayoutFormatOptions.AlignAllBottom)
    }
    
    ///左追随
    func leftSideFollowOtherView (otherView : UIView , distance : CGFloat ,viewSize : CGSize ,option : NSLayoutFormatOptions){
        self.translatesAutoresizingMaskIntoConstraints = false
        
        let dict = ["subview" : self , "otherview" : otherView]
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(\(viewSize.height))]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: dict))
        self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[otherview]-\(distance)-[subview(\(viewSize.width))]", options: option, metrics: nil, views: dict))
    }
    
    
}


